package com.guldan.jaina.manager;

import com.guldan.core.manager.BaseManager;
import com.guldan.core.utils.DateUtil;
import com.guldan.jaina.dto.projectextensiondetail.ProjectExtensionDetailDTO;
import com.guldan.jaina.dto.projectextensiondetail.ProjectExtensionDetailSearchDTO;
import com.guldan.jaina.dto.registerfail.RegisterFailDTO;
import com.guldan.jaina.utils.ProjectExtensionDetailDomainConvertUtils;
import com.guldan.jaina.utils.RegisterFailDomainConvertUtils;
import com.guldan.mapper.db.entity.ProjectExtensionDetail;
import com.guldan.mapper.db.entity.ProjectExtensionDetailExample;
import com.guldan.mapper.db.entity.ProjectExtensionDic;
import com.guldan.mapper.db.entity.RegisterFail;
import com.guldan.mapper.db.mapper.ProjectExtensionDetailMapperExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Silas.
 * Date: 2016/12/3
 * Time: 17:34
 */
@Service
public class ProjectExtensionDetailManager extends BaseManager {

    @Autowired
    private ProjectExtensionDetailMapperExt projectExtensionDetailMapperExt;

    @Autowired
    private ProjectExtensionDicManager projectExtensionDicManager;

    public List<ProjectExtensionDetailDTO> getExtDetailByRegistId(Integer registerId) {
        ProjectExtensionDetailSearchDTO searchDTO = new ProjectExtensionDetailSearchDTO();
        searchDTO.setRegisterId(registerId);
        return list(searchDTO);
    }

    public List<ProjectExtensionDetailDTO> list(ProjectExtensionDetailSearchDTO searchDTO) {
        List<ProjectExtensionDetailDTO> dtoList = new ArrayList<>();
        List<ProjectExtensionDetail> entityList = projectExtensionDetailMapperExt.selectByExample(buildSearchExample(searchDTO));
        if (entityList != null && entityList.size() > 0) {
            for (ProjectExtensionDetail entity : entityList) {

                ProjectExtensionDetailDTO dto = ProjectExtensionDetailDomainConvertUtils.convert(entity);
                dto.setExtName(projectExtensionDicManager.get(entity.getExtId()).getExtName());

                dtoList.add(dto);


            }
        }
        return dtoList;
    }

    //build查询Example
    private ProjectExtensionDetailExample buildSearchExample(ProjectExtensionDetailSearchDTO search) {
        ProjectExtensionDetailExample example = new ProjectExtensionDetailExample();
        ProjectExtensionDetailExample.Criteria criteria = example.createCriteria();

        if (search.getExtId() != null && search.getExtId() > 0) {
            criteria.andExtIdEqualTo(search.getExtId());
        }
        if (search.getRegisterId() != null && search.getRegisterId() > 0) {
            criteria.andRegisterIdEqualTo(search.getRegisterId());
        }
        if (search.getDetailId() != null && search.getDetailId() > 0) {
            criteria.andDetailIdEqualTo(search.getDetailId());
        }
        if (search.getBatchId() != null && search.getBatchId() > 0) {
            criteria.andBatchIdEqualTo(search.getBatchId());
        }


        criteria.andIsDeleteEqualTo(Boolean.FALSE);
        criteria.andDisabledEqualTo(Boolean.FALSE);
        example.setOrderByClause("ext_id asc");
        return example;
    }
    
    /**
     * save
     * @param dto
     * @return
     */
    public ProjectExtensionDetailDTO save(ProjectExtensionDetailDTO dto) {
    	ProjectExtensionDetail entity;
        if (dto.getDetailId() != null && dto.getDetailId() > 0) {
            entity = projectExtensionDetailMapperExt.selectByPrimaryKey(dto.getDetailId());
            entity.setBatchId(dto.getBatchId());
            entity.setExtContent(dto.getExtContent());
            entity.setExtId(dto.getExtId());
            entity.setProjectId(dto.getProjectId());
            entity.setRegisterId(dto.getRegisterId());
            entity.setYear(dto.getYear());
            entity.setDisabled(dto.getDisabled());
            entity.setModifyTime(DateUtil.getCurrentTime());
            entity.setModifier(dto.getModifier());
            projectExtensionDetailMapperExt.updateByPrimaryKeySelective(entity);
        } else {
            entity = new ProjectExtensionDetail();
            entity.setBatchId(dto.getBatchId());
            entity.setExtContent(dto.getExtContent());
            entity.setExtId(dto.getExtId());
            entity.setProjectId(dto.getProjectId());
            entity.setRegisterId(dto.getRegisterId());
            entity.setYear(dto.getYear());
            entity.setDisabled(dto.getDisabled());
            entity.setCreator(dto.getCreator());
            entity.setCreateTime(DateUtil.getCurrentTime());
            projectExtensionDetailMapperExt.insertSelective(entity);
        }
        dto = ProjectExtensionDetailDomainConvertUtils.convert(entity);
        return dto;
    }
    
}
